﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace ObjectModel.Excel
{
    /// <summary>
    /// Manages workbooks opened programmatically in one location and
    /// allows them to be closed with a single operation.
    /// </summary>
    public static class WorkbookManager
    {
        private static List<Workbook> mWorkbooks;

        public static void Add(Workbook workbook)
        {
            Application app = workbook.Application;
            app.Workbooks.Add();
            mWorkbooks.Add(workbook);
        }

        public static void CloseAll()
        {
            foreach (Workbook wb in mWorkbooks)
            {
                if (wb != null)
                {
                    try { wb.Close(); }
                    catch (COMException) { throw; }
                }
            }
        }
    }
}
